//
//  ViewController.m
//  CoreData
//
//  Created by z on 15/11/20.
//  Copyright © 2015年 z. All rights reserved.
//

#import "ViewController.h"
#import "Studentt.h"
#import "CoreDataManager.h"

@interface ViewController ()

- (IBAction)add:(id)sender;

@property(nonatomic, strong) CoreDataManager *manager;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建一个数据管理器
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url========  %@", [self.manager applicationDocumentsDirectory]);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



// coreData的添加方法
- (IBAction)add:(id)sender {
    
    // 1. 创建实体描述类
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Studentt" inManagedObjectContext:self.manager.managedObjectContext];
    
    
    // 2. 创建一个实体
    // 参数1: 实体描述
    // 参数2: 数据管理器
    Studentt *stu = [[Studentt alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    stu.name = @"李傻子";
    stu.age = @17;
    stu.sex = @"female";
    stu.hobby = @"逛吃";
    stu.number = @111;
    
    
    
    // 进行保存
    [self.manager saveContext];
    
}


// 查找
- (IBAction)Select:(id)sender {
    
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Studentt"];
    
    // 给请求添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"李傻子"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSLog(@"%ld", array.count);
    
}


// 修改
- (IBAction)update:(id)sender {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Studentt"];
    
    // 给请求添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"李傻子"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    
    Studentt *stu = [array lastObject];
    stu.sex = @"man";
    
    // 进行保存
    [self.manager saveContext];
    

}


// 删除
- (IBAction)delete:(id)sender {

    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Studentt" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"man"];
    [fetchRequest setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    
    for (Studentt *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
}


@end
